﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 接口实现；数据字典；
    /// </summary>
    public sealed class Source_dictDAL_impl : BaseDAL, ISource_dictDAL
    {
        public Result Add(Source_dict source_Dict)
        {
            string sql = @"INSERT INTO source_dict (
                            id,
                            dict_code,
                            item_name,
                            group_id,
                            d_sort,
                            d_stat,
                            createtime
                        )
                        VALUES (
                            $id,
                            $dict_code,
                            $item_name,
                            $group_id,
                            $d_sort,
                          1,
                            $createtime
                        );  ";
            Snowflake snowflake = new Snowflake(1, 1);
            long id = snowflake.NextId();

            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$id", id);
            keyValuePairs.Add("$dict_code", source_Dict.Dict_code);
            keyValuePairs.Add("$item_name", source_Dict.Item_name);
            keyValuePairs.Add("$group_id", source_Dict.Group_id);
            keyValuePairs.Add("$d_sort", source_Dict.D_sort);
            keyValuePairs.Add("$createtime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

            int count = base.ExecSqlCmd(sql, keyValuePairs, c =>
          {
              return c.ExecuteNonQuery();
          });
            if (count > 0)
            {
                return new Result("新增成功", 200);
            }
            return new Result("新增失败");
        }

        public Result Delete(long id)
        {
            string sql = "delete from  Source_dict  where   id=" + id;
            int count = base.ExecSqlCmd(sql, null, g =>
          {
              return g.ExecuteNonQuery();
          });
            return count > 0 ? new Result("删除成功", 200) : new Result("删除失败");
        }

        public List<Source_dict> GetAll()
        {
            string sql = "select *  from  Source_dict  ";
            return base.ExecSqlCmd(sql, null, c =>
           {
               List<Source_dict> list = new List<Source_dict>();
               var reader = c.ExecuteReader();
               while (reader.Read())
               {
                   Source_dict model = LoadModelByRead(reader);
                   list.Add(model);
               }
               reader.Close();
               reader.DisposeAsync();
               return list.OrderByDescending(g => g.D_sort).ToList();
           });
        }

        public Source_dict Get(long id)
        {
            string sql = "select *  from Source_dict  where   id=" + id;
            return base.ExecSqlCmd(sql, null, c =>
          {
              var reader = c.ExecuteReader();
              if (!reader.HasRows)
              {
                  return null;
              }
              if (reader.Read())
              {
                  Source_dict model = LoadModelByRead(reader);
                  return model;
              }
              reader.Close();
              reader.DisposeAsync();
              return null;
          });
        }

        private static Source_dict LoadModelByRead(System.Data.Common.DbDataReader reader)
        {
            Source_dict model = new Source_dict();
            model.Id = reader.GetInt64(0);
            model.Dict_code = reader["dict_code"].ToString();
            model.Item_name = reader["item_name"].ToString();
            model.Group_id = reader["group_id"].ToString();
            model.D_stat = Convert.ToByte(reader["d_stat"]);
            model.D_sort = (int)reader["D_sort"].ToMyInt64T2();
            model.Createtime = reader["createtime"].ToString();
            return model;
        }

        public List<Source_dict> GetList(string groupId, byte? stat)
        {
            string sql = "select *  from Source_dict where   group_id=$group_id  ";
            if (stat.HasValue)
            {
                sql += "  and  d_stat=" + stat;
            }
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$group_id", groupId);

            return base.ExecSqlCmd(sql, keyValuePairs, c =>
          {
              List<Source_dict> list = new List<Source_dict>();
              var reader = c.ExecuteReader();
              while (reader.Read())
              {
                  Source_dict model = LoadModelByRead(reader);
                  list.Add(model);
              }
              reader.Close();
              reader.DisposeAsync();
              return list.OrderByDescending(g=>g.D_sort).ToList();
          });
        }

        public Result Update(Source_dict source_Dict)
        {
            string sql = "  update   Source_dict  set  item_name=$item_name ,dict_code=$dict_code,group_id=$group_id,d_sort=$d_sort  where  id=" + source_Dict.Id;

            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$item_name", source_Dict.Item_name);
            keyValuePairs.Add("$group_id", source_Dict.Group_id);
            keyValuePairs.Add("$dict_code", source_Dict.Dict_code);
            keyValuePairs.Add("$d_sort", source_Dict.D_sort);

            int count = base.ExecSqlCmd(sql, keyValuePairs, g =>
          {
              return g.ExecuteNonQuery();
          });
            return count > 0 ? new Result("修改成功", 200) : new Result("修改失败");
        }

        public Result UpdateStat(long id, byte? stat)
        {
            //string sql = "update   source_dict   set  d_stat=$d_stat   where  id=" + id;
            string sql = "update   source_dict   set  d_stat=" + stat + "   where  id=" + id;
            string msg = string.Empty;
            //Dictionary<string,object> keyValuePairs = new Dictionary<string, object>();
            //keyValuePairs.Add("$d_stat", stat);

            int count = base.ExecSqlCmd(sql, null, g =>
          {
              return g.ExecuteNonQuery();
          },
            ex =>
            {
                msg = ex.Message;
            }
            );
            return count > 0 ? new Result("修改状态成功", 200) : new Result("修改失败," + msg);
        }


    }
}
